﻿using System;
using System.Collections;
using System.Reflection;
using System.Text;
using Eff.Extensions;

namespace Eff.Utils
{
    /// <summary>
    /// 调式助手
    /// </summary>
    public class DebugHelper
    {

        /// <summary>
        /// 输出obj的所有属性值
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="getPropertysOutputMode"></param>
        /// <returns></returns>
        public static string GetPropertys<T>(T obj, GetPropertysOutputMode getPropertysOutputMode = GetPropertysOutputMode.BS)
        {
            //    const int titleMaxLengh = 20;
            var breakLinkSign = string.Empty;
            var whiteSpaceSign = string.Empty;
            if (getPropertysOutputMode == GetPropertysOutputMode.BS)
            {
                breakLinkSign = "<br/>";
                whiteSpaceSign = "&emsp;";
            }
            else if (getPropertysOutputMode == GetPropertysOutputMode.CS)
            {
                breakLinkSign = "\r\n";
                whiteSpaceSign = " ";
            }
            var sb = new StringBuilder(3000);
            Type t = typeof(T);
            foreach (PropertyInfo p in t.GetProperties())
            {
                try
                {
                    string fieldName = p.Name;
                    object value = p.GetValue(obj, null);
                    sb.AppendFormat("{0}：{1}{2}", getPropertysOutputMode == GetPropertysOutputMode.BS ? "<b>" + fieldName + "</b>" : fieldName, value, breakLinkSign);
                    if (value is ICollection)
                    {
                        foreach (var item in (ICollection)value)
                        {
                            sb.AppendFormat("{0}{1}{2}", whiteSpaceSign.RepeatOuptut(3), item, breakLinkSign);
                        }
                    }
//                   else
//                    {
//                        GetPropertys(value, getPropertysOutputMode);
//                    } 
                }
                catch
                {

                }
            }
            return sb.ToString();
        }

        ///<summary>
        /// 获取属性的输出模式
        ///</summary>
        public enum GetPropertysOutputMode : byte
        {
            ///<summary>
            /// c/s 
            ///</summary>
            CS = 1,
            ///<summary>
            /// b/s
            ///</summary>
            BS = 2
        }
    }
}
